/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;

/**
 * 處理收到由客戶端傳來NPC講話動作的封包
 */
public class C_NPCTalkAction extends ClientBasePacket {

	private static final String C_NPC_TALK_ACTION = "[C] C_NPCTalkAction";
	private static Logger _log = Logger.getLogger(C_NPCTalkAction.class
			.getName());

	@Override
	public String getType() {
		return C_NPC_TALK_ACTION;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance activeChar = client.getActiveChar();
			if (activeChar == null) {
				return;
			}

			final int objectId = this.readD();
			final String action = this.readS();
			if(action == null){
				return;
			}

			final L1Object obj = L1World.getInstance().findObject(objectId);
			if (obj == null) {
				_log.warning("object not found, oid " + objectId);
				return;
			}

			try {
				final L1NpcInstance npc = (L1NpcInstance) obj;
				npc.onFinalAction(activeChar, action);
			} catch (final ClassCastException e) {
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
